package com.bayss.web.controller;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.homepage.ActivityApplyInfo;
import com.bayss.core.entity.homepage.ActivityInfo;
import com.bayss.core.entity.homepage.RecommendInfo;
import com.bayss.core.entity.homepage.RotationChart;
import com.bayss.core.service.address.AddressService;
import com.bayss.core.service.homepage.ActivityService;
import com.bayss.core.service.homepage.RecommendService;
import com.bayss.core.service.homepage.RotationChartService;
import com.bayss.core.util.CollectionUtils;
import com.bayss.web.mock.annotation.MockAble;
import com.bayss.web.mock.enums.MockDataType;
import com.bayss.web.model.homepage.ActivityInfoResp;
import com.bayss.web.model.homepage.HomepageResp;
import com.bayss.web.model.homepage.RecommendInfoResp;
import com.bayss.web.model.homepage.RotationChartResp;
import com.bayss.web.uresponse.UserResponse;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 首页
 */
@RestController
public class HomepageController extends ApiBaseController{
    @Autowired
    ActivityService activityService;
    @Autowired
    AddressService addressService;
    @Autowired
    RotationChartService rotationChartService;
    @Autowired
    RecommendService recommendService;

    /**
     * 首页信息
     *
     * @return success
     */
    @GetMapping("/homepage/info.json")
//    @MockAble(value = HomepageResp.class, type = MockDataType.LOOP)
    public ResultMsg info() {
        PageInfo<ActivityInfo> activityPages = activityService.getActivityByPage(1, 10, 1, true);
        List<ActivityInfo> activityInfos =activityPages.getList();
        PageInfo<RotationChart> chartPages = rotationChartService.getRotationChartByPage(1,1, 100);
        List<RotationChart> rotationCharts = chartPages.getList();
        List<RecommendInfo> recommendInfos = recommendService.getRecommendInfoByPage(1,1, 10);
        HomepageResp homepageResp = new HomepageResp();
        List<ActivityInfoResp> activityResps = null;
        if (CollectionUtils.isNotEmpty(activityInfos)) {
            activityResps = activityInfos.stream().map(info -> {
                ActivityInfoResp resp = new ActivityInfoResp();
                BeanUtils.copyProperties(info, resp);
                return resp;
            }).collect(Collectors.toList());
        }
        List<RotationChartResp> rotationChartResps = null;
        if (CollectionUtils.isNotEmpty(rotationCharts)) {
            rotationChartResps = rotationCharts.stream().map(info -> {
                RotationChartResp resp = new RotationChartResp();
                BeanUtils.copyProperties(info, resp);
                return resp;
            }).collect(Collectors.toList());
        }
        List<RecommendInfoResp> recommendInfoResps = null;
        if (CollectionUtils.isNotEmpty(recommendInfos)) {
            recommendInfoResps = recommendInfos.stream().map(info -> {
                 RecommendInfoResp resp = new RecommendInfoResp();
                 BeanUtils.copyProperties(info, resp);
                 return resp;
             }).collect(Collectors.toList());
        }
        homepageResp.setActivityInfoRespList(activityResps);
        homepageResp.setRecommendInfoRespList(recommendInfoResps);
        homepageResp.setRotationChartRespList(rotationChartResps);
        return ResultMsg.success( homepageResp);
    }

    /**
     * 全部活动信息
     *
     * @return success
     */
    @GetMapping("/homepage/activity/list.json")
    public ResultMsg getAllActivity(Integer page, Integer pageSize) {
        PageInfo<ActivityInfo> activityInfos = activityService.getActivityByPage(page, pageSize,1,  true);
        return ResultMsg.success( activityInfos.getList());
    }

    /**
     *
     * 活动详情
     * */
    @GetMapping("/homepage/activity/detail.json")
    public ResultMsg getAllActivity(UserResponse userResponse,Integer activityId) {
        ActivityInfo activityInfo = activityService.getActivityById(userResponse.getUserInfo(),activityId);
        return ResultMsg.success(activityInfo);
    }


    /**
     * 申请活动
     *如果申请时间不在活动的起止时间内，则申请失败
     *如果活动已失效，则申请失败
     *如果活动不存在，则申请失败
     * @return success
     */
    @PostMapping("/homepage/activity/apply.json")
    public ResultMsg apply(UserResponse userResponse, ActivityApplyInfo activityApplyInfo) {
        return activityService.addActivityApply(userResponse.getUserInfo(),activityApplyInfo);
    }


    /**
     * 添加收货地址
     */

//    @RequestMapping("/address/add")
//    public ResultMsg addressAdd(AddressInfo addressInfo) {
//        addressService.addAddress(addressInfo);
//        return new ResultMsg(true, 200, null);
//    }


    /**
     * 添加收货地址
     */

//    @RequestMapping("/address/update")
//    public ResultMsg addressUpdate(AddressInfo addressInfo) {
//        addressService.updateAddress(addressInfo);
//        return new ResultMsg(true, 200, null);
//    }


    /**
     * 添加收货地址
     */
    //TODO uid 如何获取待确定
//    @RequestMapping("/address/getAll")
//    public ResultMsg addressGetAll(Long uid) {
//        List<AddressInfo> allUserAddress = addressService.getAllUserAddress(uid);
//        List<AddressInfoResp> resps = null;
//        if (!CollectionUtils.isEmpty(allUserAddress)) {
//            resps = allUserAddress.stream().map(info -> {
//                AddressInfoResp resp = new AddressInfoResp();
//                BeanUtils.copyProperties(info, resp);
//                return resp;
//            }).collect(Collectors.toList());
//        }
//        return new ResultMsg(true, 200, resps);
//    }
}
